<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    /**
     * 首页。
     *
     * @return Response
     */
    public function index()
    {
        return view('index.index');
    }
	
    /**
     * 展示给定用户的信息。
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
		//$users = DB::select('select * from dsc_users where user_id = ?', [1]);
		$users = DB::table('users')->where('user_id', $id)->get();
		$user_info = reset($users);
		$user_info = get_object_vars($user_info);
		//var_dump($user_info);
		//$user_info = json_decode(json_encode($user_info), true);
        return view('index.index', ['user_info'=>$user_info, 'name'=>123]);
    }	
	
    /**
     * blog。
     *
     * @param  int  $id
     * @return Response
     */
    public function blog($id)
    {
		$article = DB::table('article')->where('article_id', $id)->get();
		$blog_info = reset($article);
		//$blog_info = get_object_vars($blog_info); //转为数组
        return view('index.blog', ['blog_info'=>$blog_info]);
    }	
    
    /* chart */
    public function chart()
    {
        //前后十天
        $now = time();
        $today = date('m-d', $now);
        $recent_date = array();
        for($i = -7; $i < 8; $i++){
            $this_date = $now + $i * 86400; 
            $md = date('m-d', $this_date);
            $recent_date[$this_date] = $md;
        }
        
        //统计数据
        $json_data = "";
        $data = DB::select('select * from demo_chart order by date asc');
        if($data){
            $date = array();
            $reason = array();
            $result = array();
            foreach($data as $key=>$val){
                $date[] = date('m-d', $val->date);
                $reason[] = $val->reason;
                $result[] = $val->result;
            }
            $json_data = json_encode(array($date, $reason, $result));
        }
        return view('index.chart', ['json_data'=>$json_data, 'recent_date'=>$recent_date, 'today'=>$today]);
    }
    
    /* chart */
    public function save(Request $request)
    {
        //var_dump($request->input('reason'));
        $bool=DB::insert("insert into demo_chart(id,date,reason,result)
        values(?,?,?,?)",[NULL,$request->input('date'),$request->input('reason'),$request->input('result')]);
        return redirect()->action('IndexController@chart');
    }
}